package com.example.simple.controller;

import com.example.simple.entity.RedPackage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Vector;

@RestController
@RequestMapping("/red")
public class RedController {
    Logger logger = LoggerFactory.getLogger(RedController.class);
    private List<RedPackage> listA = new Vector<>();
    private List<RedPackage> listB = new Vector<>();

    @PostMapping("/insert")
    public void addRedPackage(@RequestBody RedPackage redPackage) {
        logger.info("--新增接收红包消息：" + redPackage.toString());
        if (redPackage.getFromId().equals("171187128884098")) {
            // A号发的存到b队列
            listB.add(redPackage);
            logger.info("红包id已保存B队列, 当前共保存数目:" + listB.size());
        } else {
            // B号发的存到a队列
            listA.add(redPackage);
            logger.info("红包id已保存A队列, 当前共保存数目:" + listA.size());
        }

    }

    @GetMapping(value = "/getOneA")
    public RedPackage getRedPackageA() {
        logger.info("--获取红包id请求A");
        if(listA.size() <= 0) {
            return null;
        }
        RedPackage redPackage = listA.remove(0);
        logger.info("获取红包id A结束，获取到的红包信息:" + redPackage);
        return redPackage;
    }

    @GetMapping(value = "/getOneB")
    public RedPackage getRedPackageB() {
        logger.info("--获取红包id请求B");
        if(listB.size() <= 0) {
            return null;
        }
        RedPackage redPackage = listB.remove(0);
        logger.info("获取红包id B结束，获取到的红包信息:" + redPackage);
        return redPackage;
    }
}
